package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.CommodityType;
import com.olinsports.system.olinsportssystem.model.InventoryDetails;
import com.olinsports.system.olinsportssystem.model.InventorySettings;
import com.olinsports.system.olinsportssystem.model.MultiCondition;
import com.olinsports.system.olinsportssystem.repository.InventorySettingsRepository;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class InventorySettingsService {
    @Autowired
    private InventorySettingsRepository inventorySettingsRepository;
    @Autowired
    private CommodityTypeService commodityTypeService;
    @Autowired
    private InventoryDetailsService inventoryDetailsService;
    private int totalPages = 0;//总页数

    private int  totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;

    public List<InventorySettings> selectAll(){
        return inventorySettingsRepository.selectAll();
    }
    public int insertAll(InventorySettings inventorySettings){
        return inventorySettingsRepository.insertAll(inventorySettings);
    }
    public int deleteByKey(int detailedKey){
        return inventorySettingsRepository.deleteByKey(detailedKey);
    }

    public int updateData(InventorySettings inventorySettings){
        return inventorySettingsRepository.updateData(inventorySettings);
    }

    public InventorySettings selectByKey(int inventoryKey){
        return inventorySettingsRepository.selectByKey(inventoryKey);
    }

    public Map listOrSearchInvent(String page, String rows, boolean _search, String sord, String sidx){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        if(_search){//进行查找操作
            List<MultiCondition> multiList = pageUtil.searchList(page,rows,_search,request);

            //通过条件进行查询总数据量
            totalRows =  inventorySettingsRepository.findByParamsCount(multiList);

            System.out.println("totalRows = "+totalRows);
            //通过条件查询beg-rows的数据，分页查询
            List<InventorySettings> stmCountSearch = inventorySettingsRepository.findByParamsLimit(sord,sidx,multiList,beg,Integer.valueOf(rows));
            Map mapSearch =pageUtil.paging(rows,page,totalRows);
            //设置表格数据
            mapSearch.put("rows",stmCountSearch);
            return mapSearch;
        } else {
            //查询当前表中的总数据量
            totalRows = inventorySettingsRepository.findCount();
            System.out.println("totalRows = "+totalRows);
            //通过分页进行查询当前表中的数据
            List<InventorySettings> stmCountList =  inventorySettingsRepository.findLimit(sord,sidx,beg,Integer.valueOf(rows));

            Map mapList = pageUtil.paging(rows,page,totalRows);
            mapList.put("rows",stmCountList);//设置行显示的数据

            return mapList;
        }
    }

    public int saveInventorySettings(InventorySettings inventorySettings,int typekey) {
        CommodityType commodityType = commodityTypeService.selectByTypeKey(typekey);
        Date date = new Date(System.currentTimeMillis());//获取当前时间


        InventoryDetails inventoryDetails = new InventoryDetails();
        inventoryDetails.setSku(inventorySettings.getSku());
        inventoryDetails.setName(inventorySettings.getName());
        inventoryDetails.setType(inventorySettings.getType());
        inventoryDetails.setQuantity(inventorySettings.getQuantity());
        inventoryDetails.setDate(date);
       // inventoryDetails.setOperatorNum(inventorySettings.getOperatorNum().get);
        inventoryDetails.setStock(inventorySettings.getStock());
        inventoryDetails.setSkuType(commodityType);
        inventoryDetails.setAmdLog(date);

        inventoryDetails.setRemarks(inventorySettings.getRemarks());
        inventoryDetails.setPrice(inventorySettings.getPrice());



        inventorySettings.setAmdLog(date);
        inventorySettings.setDate(date);
        inventorySettings.setSkuType(commodityType);

        inventoryDetailsService.insertAll(inventoryDetails);

       return inventorySettingsRepository.updateData(inventorySettings);
    }
}
